package work.chenbo.springboot.jap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import work.chenbo.springboot.jap.dao.UserCrudRepository;
import work.chenbo.springboot.jap.dao.UserPageAndSortingRepository;
import work.chenbo.springboot.jap.entity.Users;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

/**
 * @className UserController
 * @authtor ChenBo
 * @date 2020/4/15
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserCrudRepository userCrudRepository;

    @Autowired
    private UserPageAndSortingRepository userPageAndSortingRepository;

    /**
    * 添加用户
    * @author; ChenBo
    * @datetime: 2020/4/15
    */
    @GetMapping("/add")
    public @ResponseBody Users addNewUser(@RequestParam String username,@RequestParam String password,@RequestParam String nickName){
        Users users = new Users();
        users.setCreateBy(1);
        users.setCreateTime(new Date());
        users.setNickName(nickName);
        users.setUsername(username);
        users.setPassword(password);
        users.setStatus(1);
        return userCrudRepository.save(users);
    }

    /**
    * 查询所有用户
    * @author; ChenBo
    * @datetime: 2020/4/15
    */
    @GetMapping("/all")
    public @ResponseBody Iterable<Users> getAll(){
        return userCrudRepository.findAll();
    }

    /**
    * 根据id查询
    * @author; ChenBo
    * @datetime: 2020/4/15
    */
    @GetMapping("/info")
    public @ResponseBody Users findOne(@RequestParam Integer id){
        return userCrudRepository.findById(id).orElse(null);
    }

    /**
    * 根据ID删除
    * @author; ChenBo
    * @datetime: 2020/4/15
    */
    @RequestMapping("/del")
    public @ResponseBody Integer delete(@RequestParam Integer id){
        userCrudRepository.deleteById(id);
        return  1;
    }

}
